import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;

import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.RestClientException;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.domain.Version;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;



/*@Sangeeta
 *@Uses: Program to get version of the bug report for analysis
 */

public class get_version 
{
	public Connection conn =  null;
	 public String dbname="2jboss";
	 public String userid="root";
	 public String passwd = "123";
	 public static ArrayList<String> bugids =  null;
	 public ArrayList<String> nfids =  null;
	// public String file_path ="/home/sangeeta/2Research/jboss_useful_data/selected_bugid.csv";
	// public String nfid_path="/home/sangeeta/2Research/jboss_useful_data/selected_files.csv";
	 
public ArrayList<String> getBugids(Connection conn)
{
		 bugids =  new ArrayList<String>();
	
		 Statement stmt =  null;
		 String bug_q= "select * from selected_bugs";
		 
		 try 
		 {
			
			 stmt =  conn.createStatement();
			 stmt.execute(bug_q);
			 ResultSet rs = stmt.getResultSet();
			 while(rs.next())
			 {
				 String bugid =  rs.getString("bugid");
				 bugids.add(bugid);
			 }		
			 
		  } catch (SQLException e) 
		  {	e.printStackTrace(); }
		  
		/* for(int i=0;i<bugids.size();i++)
		 {
			 System.out.println("bugids="+bugids.get(i));
		 }*/
		  
		  
return bugids;		  
		 
}//process

//1. connect to JIRA
//2. Get the version
//3. insert the version to  table
private void process(ArrayList<String> bugids,Connection conn) 
{
	
	try{
		
		
		JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
        URI jiraServerUri = new URI("https://issues.jboss.org");
        JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, "sangeetalal", "iiitd123");
        NullProgressMonitor pm = new NullProgressMonitor();
        
        System.out.println("Size="+bugids.size());
        
        int i = 0;//bugids.indexOf("JBAS-4399");
        
        for(i=0;i<bugids.size();i++)
        {
        	System.out.println("count="+i);
        	
        	try
        	{
        		
        		String issueString =  bugids.get(i); //JBAS-123
        		System.out.println("IssueId="+issueString);
        		
        		Issue issue = restClient.getIssueClient().getIssue(issueString, pm);     	
        		String issueKey = issue.getKey();
        		//System.out.println("ISSUE KEY : " + issueKey); 
        		        		
        		Iterable<Version> version  = issue.getAffectedVersions();
        		Iterator itr=version.iterator();
        		
        		if(!itr.hasNext())
        		 {
        			System.out.println("Dont have");
        			java.sql.Timestamp default_date = java.sql.Timestamp.valueOf("1999-31-12 10:00:00.0");
        			insert_to_db(issueString,"0","0","0","0",conn);
        			
        		 }//if
        		
        		else
        		{
        		
        			while(itr.hasNext())
        			{
        				System.out.println("------------");
        			
        				System.out.println("ISSUE KEY : " + issueKey); 
        			
        				String itr_str=itr.next().toString();
        				System.out.println(itr_str);
        			
        				int s1= itr_str.indexOf("name=");
        				int e1 = itr_str.indexOf(",", s1);
        				String rev_name =itr_str.substring(s1+5, e1);
        				System.out.println("rev_name="+rev_name);
        			
        				int s2 = itr_str.indexOf("description=");
        				int e2=itr_str.indexOf("isArchived", s2);
        				String rev_desc = itr_str.substring(s2+12, e2);
        				rev_desc = rev_desc.replaceAll(",", " ");
        				System.out.println("rev_desc="+rev_desc);
        			
        			
        				int s3 = itr_str.indexOf("isArchived=");
        				int e3 = itr_str.indexOf(",", s3);
        				String isArchived = itr_str.substring(s3+11, e3);
        				System.out.println("isArchived ="+isArchived);
        			
        				int s4 = itr_str.indexOf("isReleased=");
        				int e4 = itr_str.indexOf(",", s4);
        				String isReleased = itr_str.substring(s4+11, e4);
        				System.out.println("isReleased="+isReleased);
        			
        				int s5=itr_str.indexOf("releaseDate=");
        				int e5 = itr_str.indexOf("+",s4);
        				
        				String releaseDate_str ="";
        				if(e5!=-1)
        					{
        					 String release_str = itr_str.substring(s5+12, e5);
        					 releaseDate_str = release_str.replace("T", " ").trim(); 
        					 releaseDate_str = releaseDate_str.replace(".000", ".0");
        					//  System.out.println("date str="+releaseDate_str);
        					}//if
        				else
        				 	{
        					  releaseDate_str = "1999-31-12 10:00:00.0";  
        				 	 
        				 	}
        		    
        			    java.sql.Timestamp releaseDate =java.sql.Timestamp.valueOf(releaseDate_str); 	
        			    System.out.println("releaseDate="+releaseDate);
        		
        			    insert_to_db(issueString,rev_name,rev_desc,isArchived,isReleased,conn);
        			    System.out.println("-----------");
        			    //intr();
        		
        			
        		}//while
        			
        	}//else		
        		
        		
        		
        	}catch(RestClientException e)
        		{
        			System.out.println("Rest client Exception");
        			
        		}
  	        
        }//for
               
	}
	catch(RestClientException e)
	 {
		e.printStackTrace();
	 	System.out.println("Rest Exception caught");
	 }
	catch(Exception e){
		System.out.println("Caught");
		e.printStackTrace();
	}
	
}//process

private void insert_to_db(String bugid,String rev_name, String rev_desc, String isArchived,String isReleased, 
		 Connection conn) 
{
 	
   String insert="insert into bug_version values('"+bugid+"','"+rev_name+"','"+rev_desc+"','"+isArchived+"','"
  +isReleased+"')";  
   System.out.println("insert="+insert);
   
    Statement stmt = null;
	try {
		
		  stmt = conn.createStatement();
		  stmt.executeUpdate(insert);
	
	  } catch (SQLException e) 
	  {	e.printStackTrace();}
 
	
}//insert

public void intr()
 {
		 BufferedReader br =  new BufferedReader(new InputStreamReader(System.in));
		 System.out.println("INTR");
		 try {
			br.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
}
	 
public static void main(String args[])
{
			get_version obj =  new get_version();
			db_conn db=  new db_conn();
			obj.conn = db.connect(obj.dbname, obj.userid, obj.passwd);
			obj.bugids=obj.getBugids(obj.conn);
			obj.process(bugids,db.conn);
			db.close(obj.conn);	     		
}//main


}//get_version
